package cn.wolfcode.wms.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.wolfcode.wms.domain.OrderBill;
import cn.wolfcode.wms.domain.OrderBillItem;
import cn.wolfcode.wms.mapper.OrderBillItemMapper;
import cn.wolfcode.wms.mapper.OrderBillMapper;
import cn.wolfcode.wms.query.PageResult;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.IOrderBillService;
import cn.wolfcode.wms.util.UserContext;

@Service
public class OrderBillServiceImpl implements IOrderBillService {

	@Autowired
	private OrderBillMapper mapper;
	
	@Autowired 
	private OrderBillItemMapper itemMapper;
	
	@Override
	public void insert(OrderBill orderBill) {
		orderBill.setInputUser(UserContext.getUser());
		orderBill.setInputTime(new Date());
		BigDecimal totalAmount = BigDecimal.ZERO;
		BigDecimal totalNumber = BigDecimal.ZERO;
		for (OrderBillItem intem : orderBill.getItems()) {
			intem.setAmount(intem.getCostPrice().multiply(intem.getNumber()));
			totalAmount= totalAmount.add(intem.getAmount());
			totalNumber= totalNumber.add(intem.getNumber());
		}
		orderBill.setTotalAmount(totalAmount);
		orderBill.setTotalNumber(totalNumber);
		mapper.insert(orderBill);
		
		Long billId = orderBill.getId();
		for (OrderBillItem intem : orderBill.getItems()) {
			intem.setBillId(billId);
			itemMapper.insert(intem);
		}
	}

	@Override
	public void deleteByPrimaryKey(Long id) {
		mapper.deleteByPrimaryKey(id);
		itemMapper.deleteByBillId(id);
	}

	@Override
	public void updateByPrimaryKey(OrderBill orderBill) {
		//删除旧的明细
		itemMapper.deleteByBillId(orderBill.getId());
		
		BigDecimal totalAmount = BigDecimal.ZERO;
		BigDecimal totalNumber = BigDecimal.ZERO;
		for (OrderBillItem intem : orderBill.getItems()) {
			intem.setAmount(intem.getCostPrice().multiply(intem.getNumber()));
			totalAmount= totalAmount.add(intem.getAmount());
			totalNumber= totalNumber.add(intem.getNumber());
		}
		orderBill.setTotalAmount(totalAmount);
		orderBill.setTotalNumber(totalNumber);
		//修改订单信息
		mapper.updateByPrimaryKey(orderBill);
		
		Long billId = orderBill.getId();
		for (OrderBillItem intem : orderBill.getItems()) {
			intem.setBillId(billId);
			//保存新的明细
			itemMapper.insert(intem);
		}
	}

	@Override
	public OrderBill selectByPrimaryKey(Long id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<OrderBill> selectAll() {
		return mapper.selectAll();
	}

	@Override
	public PageResult query(QueryObject qo) {
		int totalCount = mapper.queryForCount(qo);
		if (totalCount == 0) {
			return new PageResult(qo.getPageSize());
		}
		List<OrderBill> listData= mapper.queryForList(qo);
		return new PageResult(qo.getCurrentPage(),qo.getPageSize(), listData, totalCount);
	}

	@Override
	public void auditByPrimaryKey(Long id) {
		//根据id查询订单
		OrderBill orderBill = mapper.selectByPrimaryKey(id);
		if (orderBill.getStatus()==orderBill.STATUS_NORMAL) {
			//修改状态
			orderBill.setStatus(1);
			//设置审核人
			orderBill.setAuditor(UserContext.getUser());
			//设置审核时间
			orderBill.setAuditTime(new Date());
			//更新数据
			mapper.updateStatus(orderBill);
		}
	}

}
